<template>
  <div class="login-container">
    <div class="login-weaper  animated bounceInDown">

      <div class="login-left animate__animated animate__fadeInLeft">
        <img class="img" src="@/assets/img/logo.png" alt=""/>
        <p class="title">系统名称</p>
      </div>
      <div class="login-border animate__animated animate__fadeInRight">
        <div class="login-main">
          <el-form
              ref="loginForm"
              class="login-form"
              status-icon
              :rules="loginRules"
              :model="loginForm"
              label-width="0"
          >
            <el-form-item prop="username">
              <el-input
                  v-model="loginForm.username"
                  auto-complete="off"
                  placeholder="请输入用户名"
                  @keyup.enter.native="handleLogin"
              >
                <template #prefix>
                  <i class="el-icon-user"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                  v-model="loginForm.password"
                  size="small"
                  type="password"
                  auto-complete="off"
                  show-password
                  placeholder="请输入密码"
                  @keyup.enter.native="handleLogin"
              >
                <template #prefix>
                  <i class="el-icon-lock"></i>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item v-if="false" prop="code">
              <el-input
                  v-model="loginForm.code"
                  :maxlength="code.len"
                  auto-complete="off"
                  placeholder="请输入验证码"
                  @keyup.enter.native="handleLogin"
              >
                <template #prefix>
                  <i class="icon-yanzhengma"></i>
                </template>
                <template #append>
                  <div class="login-code">
                    <img
                        :src="code.src"
                        class="login-code-img"
                        @click="getCode()"
                    />
                  </div>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                  type="primary"
                  class="login-submit"
                  :loading="btnLoading"
                  @click.native.prevent="handleLogin"
              >登录
              </el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'login',
  components: {},
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: 'Admin213',
      },
      checked: false,
      btnLoading: false,
      code: {
        src: '',
        value: '',
        len: 4,
        type: 'image'
      },
      loginRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '密码长度最少为6位', trigger: 'blur'}
        ],
        code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    getCode() {
      // getCodeImg().then((response) => {
      //   this.code.src = "data:image/gif;base64," + response.data.data.img;
      //   this.loginForm.uuid = response.data.data.uuid;
      // }).catch(() => {
      // })
    },
    async handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.btnLoading = true
          this.$api.api_urcap_login({
            param: this.loginForm
          }).then(async (res) => {
            localStorage.setItem('token', JSON.stringify(res.data.token))
            this.getUserInfo()
            await this.getMenu()
            this.$router.push({path: '/'})
          }).finally(() => {
            this.btnLoading = false
          })
        }
      })
    },
    getUserInfo() {
      this.$api.api_urcap_auth_user_getCurrentUser().then(res => {
        localStorage.setItem('userInfo',JSON.stringify(res.data))
      })
    },
    // 获取用户权限
    async getMenu() {
      await this.$api
          .api_urcap_auth_index({})
          .then(res => {
            this.menuPermission = res.data[0]
            let array = traverseTree(this.menuPermission, [])
            localStorage.setItem('menuPermission', JSON.stringify(array))
          })

      function traverseTree(node, array) {
        if (node === null) return;  // 当前节点为空，则直接返回
        array.push(node.path)
        if (node?.children?.length) {
          for (let i = 0; i < node.children.length; i++) {
            traverseTree(node.children[i], array);  // 递归遍历子节点
          }
        }
        return array
      }
    },
  },
  created() {
  },
}
</script>

<style scoped lang="scss">
.login-container {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #fff;
  overflow: hidden;

  &::before {
    content: ' ';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    margin-left: -48%;
    background-image: url('../../assets/img/login-bg.svg');
    background-position: 100%;
    background-repeat: no-repeat;
    background-size: auto 100%;
  }

  .el-input-group__append {
    padding: 0;
    background-color: #fff;
  }

  .el-form-item {
    margin-bottom: 20px !important;
  }
}

.login-weaper {
  margin: 0 auto;
  width: 100%;
  height: 100%;
}

.login-left,
.login-border {
  position: relative;
  //min-height: 500px;
  align-items: center;
  display: flex;
}

.login-left {
  justify-content: center;
  align-items: center;
  flex-direction: column;
  color: #fff;
  float: left;
  width: 660px;
  height: 100%;
  position: relative;
  box-sizing: border-box;
}

.login-left .img {
  width: 200px;
  height: 200px;
}

.login-time {
  position: absolute;
  left: 25px;
  top: 25px;
  width: 100%;
  color: #fff;
  opacity: 0.9;
  font-size: 18px;
  overflow: hidden;
  font-weight: bold;
}

.login-left .title {
  margin-top: 60px;
  text-align: center;
  color: #fff;
  letter-spacing: 2px;
  font-size: 28px;
  font-weight: bold;
}

.login-border {
  border-left: none;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  color: #fff;
  width: calc(100% - 660px);
  height: 100%;
  float: left;
  box-sizing: border-box;
}

.login-main {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  margin: 0 auto;
  padding: 30px 50px;
  width: 70%;
  //box-shadow: -4px 5px 10px rgba(255, 255, 255, 0.4);
  box-sizing: border-box;
}

.login-main > h3 {
  margin-bottom: 20px;
}

.login-title {
  color: #000;
  margin-bottom: 50px;
  font-weight: bold;
  font-size: 28px;
  text-align: center;
  letter-spacing: 4px;
}

.login-menu {
  margin-top: 40px;
  width: 100%;
  text-align: center;

  a {
    color: #999;
    font-size: 12px;
    margin: 0px 8px;
  }
}

.login-submit {
  width: 100%;
  height: 45px;
  font-size: 18px;
  letter-spacing: 2px;
  font-weight: 300;
  cursor: pointer;
  margin-top: 30px;
  transition: 0.25s;
  color: #FFFFFF;
  border: none;
  background: #2774F4;

  &:hover {
    background: #0B085F;
  }
}

.login-form {
  margin: 10px 0;

  i {
    color: #333;
  }

  .el-form-item__content {
    width: 100%;
  }

  .el-form-item {
    margin-bottom: 12px;
  }

  .el-input {
    input {
      padding: 20px 0;
      border-radius: 0;
    }

    .el-input__suffix, .el-input__prefix {
      display: flex;
      align-items: center;
      text-align: center;
    }

    .el-input__prefix {
      margin-left: 8px;
    }
  }
}

.login-code {
  width: 100%;
  display: flex;
  align-items: center;

  .el-input {
    flex: auto
  }

  img {
    padding: 0 10px;
  }
}

.login-code-img {
  width: 100px;
  height: 40px;
  background-color: #fdfdfd;
  border: 1px solid #f0f0f0;
  color: #333;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 5px;
  line-height: 38px;
  text-indent: 5px;
  text-align: center;
  cursor: pointer !important;
}

</style>